package com.yoda.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.youdianda.common.R;
import com.yoda.youdianda.common.RC;
import com.yoda.youdianda.common.ThreadContext;
import com.yoda.youdianda.domain.YddArticle;
import com.yoda.youdianda.domain.YddCate;
import com.yoda.youdianda.dto.YddArticleDto;
import com.yoda.youdianda.mapper.YddArticleMapper;
import com.yoda.youdianda.mapper.YddCateMapper;
import com.yoda.youdianda.service.YddArticleService;
import com.yoda.youdianda.service.YddCateService;
import com.yoda.youdianda.vo.ArticleVo;
import com.yoda.youdianda.vo.PageResult;
import com.yoda.youdianda.vo.RArticlessVo;
import com.yoda.youdianda.vo.RAticlesVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 11月 13日 14:50
 */
@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {

    @Autowired
    private YddArticleMapper articleMapper;



    @Resource
    private YddCateMapper yddCateMapper;

    @Override
    public List<YddArticle> getAllArticles() {
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(YddArticle::getStatus,3);
        wrapper.orderByDesc(YddArticle::getCreateDate).orderByDesc(YddArticle::getUpdateDate);
        return articleMapper.selectList(wrapper);
    }

    /**
     * 前台-推荐文章
     *
     * @return
     */
    @Override
    public RArticlessVo recommend() {
        List<YddArticle> allArticles = this.getAllArticles();
        ArrayList<RAticlesVo> arrayList = new ArrayList<>();
        for (YddArticle yddArticle : allArticles) {
            Integer cateid = yddArticle.getCateid();
            YddCate yddCate = yddCateMapper.selectById(cateid);
            String catename = yddCate.getCatename();

            RAticlesVo rAticlesVo = new RAticlesVo();
            BeanUtils.copyProperties(yddArticle, rAticlesVo);
            rAticlesVo.setCatename(catename);
            arrayList.add(rAticlesVo);
        }
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(YddArticle::getCreateDate).last("limit 6");
        RArticlessVo rArticlessVo = new RArticlessVo();
        rArticlessVo.setDesc("推荐文章");
        rArticlessVo.setList(arrayList);
        return rArticlessVo;
    }

    @Override
    public Integer selectCount(LambdaQueryWrapper<YddArticle> lqw) {
        Integer integer = articleMapper.selectCount(lqw);
        return integer;
    }


    @Override
    public void insert(YddArticleDto yddArticleDto) {
        YddArticle yddArticle = YddArticleDto.init(yddArticleDto);
        articleMapper.insert(yddArticle);
    }

    @Override
    public void deleteById(int id) {
        YddArticle yddArticle = new YddArticle();
        yddArticle.setId(id);
        yddArticle.setStatus(3);
        articleMapper.updateById(yddArticle);
    }

    /**
     * 分页查询个人文章
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param id       id
     * @return {@link PageResult}<{@link ArticleVo}>
     */
    @Override
    public PageResult<ArticleVo> selectByPage(Integer page, Integer pageSize, Integer id) {
        Page<YddArticle> articlePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getUserId, id);
        wrapper.ne(YddArticle::getStatus, 3);
        wrapper.orderByDesc(YddArticle::getCreateDate).orderByDesc(YddArticle::getUpdateDate);

        Page<YddArticle> selectPage = articleMapper.selectPage(articlePage, wrapper);
        List<YddArticle> records = selectPage.getRecords();
        if (Objects.isNull(records) || records.size() == 0) {
            return null;
        }
        List<ArticleVo> articleVos = records.stream().map(ArticleVo::init).collect(Collectors.toList());
        return new PageResult<>(page, pageSize, (int) selectPage.getTotal(), articleVos);
    }

    @Override
    public R<String> upd(YddArticleDto yddArticleDto) {

        YddArticle yddArticle = YddArticleDto.init(yddArticleDto);

         articleMapper.updateById(yddArticle);

         return R.success("");
    }

    @Override
    public List<YddArticle> getYddArticle(List<Integer> articleIds) {
        List<YddArticle> yddArticles = articleMapper.selectBatchIds(articleIds);
        return yddArticles;
    }
}
